﻿#region Disclaimer / License
// Copyright (C) 2011, Kenneth Bergeron, IAP Worldwide Services, Inc
// NOAA :: National Marine Fisheries Service
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataSets
{
    /// <summary>
    /// Post local XML to SDES Access form
    /// </summary>
    public partial class PostFishDialog : Form
    {
        /// <summary>
        /// Cruise number
        /// </summary>
        public string Cruise { get; set; }
        /// <summary>
        /// Vessel ID
        /// </summary>
        public string VesselCode { get; set; }
        /// <summary>
        /// Target SDES Access database
        /// </summary>
        public string Target { get; private set; }
        /// <summary>
        /// Title
        /// </summary>
        public string VesselName { set { this.Text = "Post  " + value; } }
        /// <summary>
        /// Has been posted
        /// </summary>
        private bool Posted = false;
        /// <summary>
        /// Has been saved
        /// </summary>
        private bool Saved = false;
        /// <summary>
        /// Provide help
        /// </summary>
        public delegate void HelpDelegate();
        /// <summary>
        /// Provide help
        /// </summary>
        private HelpDelegate Helper;
        /// <summary>
        /// Provide help
        /// </summary>
        public HelpDelegate HelpMe
        {
            get { return Helper; }
            set { this.helpToolStripButton.Visible = true; Helper = value; }
        }
        /// <summary>
        /// Post local XML to SDES Access form
        /// </summary>
        public PostFishDialog(string aVesselCode, string aCruise)
        {
            if (!System.IO.File.Exists(DataSets.Glob.EASADatabase))
            {
                if (!System.IO.File.Exists(DataSets.Glob.EASADatabase))
                {
                    MessageBox.Show("The database " + DataSets.Glob.EASADatabase + " does not exist.\nReinstalling the application may correct this.");
                    return;
                }
                System.IO.File.Copy(DataSets.Glob.GetEASATemplate(), DataSets.Glob.EASADatabase);
                // Makes the connection string
                Properties.Settings.Default.SetEASADatabase(DataSets.Glob.EASADatabase);
            }
            InitializeComponent();
            this.SDESToolStripLabel.Text += ": " + DataSets.Glob.EASADatabase;
            this.LocalToolStripLabel.Text += ": " + LocalDataSet.DefaultXMLFile;
            foreach (DataTable T in this.EASADataSet.Tables)  // Oh you little...
                foreach (DataColumn C in T.Columns)
                    C.AutoIncrementSeed = C.AutoIncrementStep = 1;
            VesselCode = aVesselCode;
            this.Cruise = aCruise;
            this.Target = Target;
        }
        /// <summary>
        /// Fill the SDES Access tables
        /// </summary>
        private void PostFishDialog_Load(object sender, EventArgs e)
        {
            this.catchTableAdapter.Fill(this.EASADataSet._catch);
            this.tagnumbersTableAdapter.Fill(this.EASADataSet.tagnumbers);
            this.stationsTableAdapter.Fill(this.EASADataSet.stations);
            this.samplesTableAdapter.Fill(this.EASADataSet.samples);
            this.lengthfreqTableAdapter.Fill(this.EASADataSet.lengthfreq);
            this.lengthdetailsTableAdapter.Fill(this.EASADataSet.lengthdetails);
            this.eventsTableAdapter.Fill(this.EASADataSet.events);
            this.localDataSet.Load();
        }
        /// <summary>
        /// Save the SDES Access database
        /// </summary>
        private void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            this.tableAdapterManager.UpdateAll(this.EASADataSet);
            MessageBox.Show("Saved " + Target);
        }
        /// <summary>
        /// Show a progress bar
        /// </summary>
        private void Progress(object Sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            this.toolStripProgressBar1.Value = e.ProgressPercentage % this.toolStripProgressBar1.Maximum;
            Application.DoEvents();
            // If this happens too fast, people don't belive it happened, so slow it down.
            System.Threading.Thread.Sleep(500);
        }
        /// <summary>
        /// Post clicked, go for it
        /// </summary>
        private void PostToolStripButton_Click(object sender, EventArgs e)
        {
            this.toolStripProgressBar1.Visible = true;
            PostFish.Post(VesselCode, Cruise, localDataSet, EASADataSet, Progress);
            this.toolStripProgressBar1.Visible =false;
            this.PostToolStripButton.Visible = false;
            this.Posted = true;
            this.tableAdapterManager.UpdateAll(this.EASADataSet);
            this.Saved = true;
        }
        /// <summary>
        /// Save SDES as a different name/place
        /// </summary>
        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!System.IO.File.Exists(Target)) return;
            this.saveFileDialog1.FileName = System.IO.Path.GetFileName( Target );
            if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.IO.File.Copy(Target, this.saveFileDialog1.FileName);
                this.Saved = true;
            }
        }
        /// <summary>
        /// Closing, issue warning
        /// </summary>
        private void PostFishDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel =
                e.CloseReason == CloseReason.UserClosing && Posted && !Saved &&
                MessageBox.Show("The SDES catch database was not saved, exit anyway?", "SAVED", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No;
        }
        /// <summary>
        /// Pressed exit
        /// </summary>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        /// <summary>
        /// Pressed Help
        /// </summary>
        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            HelpMe();
        }
    }
}
